home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 …ember: Reference Library / Dev.CD Dec 97 RL.toast / What's New / Tool Chest / Testing & Debugging / Virtual User / Examples / External Tool Templates / CPlus Tool Template / ScriptValue.cp < prev    next >
Encoding:
Text File  |  1997-10-15  |  13.2 KB  |  470 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        ScriptValue.cp
  3.  *
  4.  *    Contains:    xxx put contents here xxx
  5.  *
  6.  *    Written by:    Rick Violet
  7.  *
  8.  *    Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  9.  *
  10.  *    Change History (most recent first):
  11.  *
  12.  *         <4>      7/8/93    RV        Fix uninitialized fListHead in VUList class
  13.  *        <3+>      7/8/93    RV        Fix uninitialized fListHead in VUList class
  14.  *                11/18/92    RV        xxx put comment here xxx
  15.  *
  16.  *    To Do:
  17.  */
  18.  
  19. #ifndef        __ScriptValue__
  20. #include        "ScriptValue.h"
  21. #endif
  22.  
  23. //—————————————————————————————————————————————————————————————————————————————————————
  24. //                                ScriptValue
  25. //—————————————————————————————————————————————————————————————————————————————————————
  26. //    ScriptValue::ScriptValue    -    constructor.
  27. //—————————————————————————————————————————————————————————————————————————————————————
  28. ScriptValue::ScriptValue()
  29. {
  30.     fNextValue = nil;
  31.     fValueKind = kVUNullKind;
  32. }
  33.  
  34. //—————————————————————————————————————————————————————————————————————————————————————
  35. //    ScriptValue::Clone    -    returns a copy of the ScriptValue object
  36. //—————————————————————————————————————————————————————————————————————————————————————
  37. ScriptValue*
  38. ScriptValue::Clone()
  39. {
  40.     return new ScriptValue();
  41. }
  42.  
  43. //—————————————————————————————————————————————————————————————————————————————————————
  44. //                                VUNull
  45. //—————————————————————————————————————————————————————————————————————————————————————
  46. //    VUNull::VUNull    -    constructor.
  47. //—————————————————————————————————————————————————————————————————————————————————————
  48. VUNull::VUNull()
  49. {
  50.     fValueKind = kVUNullKind;
  51. }
  52.  
  53. //—————————————————————————————————————————————————————————————————————————————————————
  54. //    VUNull::Clone    -    returns a copy of the VUNull object
  55. //—————————————————————————————————————————————————————————————————————————————————————
  56. ScriptValue*
  57. VUNull::Clone()
  58. {
  59.     return new VUNull();
  60. }
  61.  
  62. //—————————————————————————————————————————————————————————————————————————————————————
  63. //                                VUBoolean
  64. //—————————————————————————————————————————————————————————————————————————————————————
  65. //    VUBoolean::VUBoolean    -    constructor.
  66. //—————————————————————————————————————————————————————————————————————————————————————
  67. VUBoolean::VUBoolean( Boolean    pFlag )
  68. {
  69.     fValueKind = kVUBooleanKind;
  70.     fFlag = pFlag;
  71. }
  72.  
  73. //—————————————————————————————————————————————————————————————————————————————————————
  74. //    VUBoolean::GetBoolean    -    returns the value of the Boolean.
  75. //—————————————————————————————————————————————————————————————————————————————————————
  76. Boolean
  77. VUBoolean::GetBoolean()
  78. {
  79.     return fFlag;
  80. }
  81.  
  82. //—————————————————————————————————————————————————————————————————————————————————————
  83. //    VUBoolean::Clone    -    returns a copy of the VUBoolean object
  84. //—————————————————————————————————————————————————————————————————————————————————————
  85. ScriptValue*
  86. VUBoolean::Clone()
  87. {
  88.     return new VUBoolean( fFlag );
  89. }
  90.  
  91. //—————————————————————————————————————————————————————————————————————————————————————
  92. //                                VUNumber
  93. //—————————————————————————————————————————————————————————————————————————————————————
  94. //    VUNumber::VUNumber    -    constructor.
  95. //—————————————————————————————————————————————————————————————————————————————————————
  96. VUNumber::VUNumber( short    pNumber )
  97. {
  98.     fValueKind = kVUNumberKind;
  99.     fNumber = pNumber;
  100. }
  101.  
  102. //—————————————————————————————————————————————————————————————————————————————————————
  103. //    VUNumber::GetNumber    -    returns the value of the number.
  104. //—————————————————————————————————————————————————————————————————————————————————————
  105. short
  106. VUNumber::GetNumber()
  107. {
  108.     return fNumber;
  109. }
  110.  
  111. //—————————————————————————————————————————————————————————————————————————————————————
  112. //    VUNumber::Clone    -    returns a copy of the VUNumber object
  113. //—————————————————————————————————————————————————————————————————————————————————————
  114. ScriptValue*
  115. VUNumber::Clone()
  116. {
  117.     return new VUNumber( fNumber );
  118. }
  119.  
  120. //—————————————————————————————————————————————————————————————————————————————————————
  121. //                                VULongNumber
  122. //—————————————————————————————————————————————————————————————————————————————————————
  123. //    VULongNumber::VULongNumber    -    constructor.
  124. //—————————————————————————————————————————————————————————————————————————————————————
  125. VULongNumber::VULongNumber( long    pNumber )
  126. {
  127.     fValueKind = kVULongNumberKind;
  128.     fNumber = pNumber;
  129. }
  130.  
  131. //—————————————————————————————————————————————————————————————————————————————————————
  132. //    VULongNumber::GetNumber    -    returns the value of the number.
  133. //—————————————————————————————————————————————————————————————————————————————————————
  134. long
  135. VULongNumber::GetNumber()
  136. {
  137.     return fNumber;
  138. }
  139.  
  140. //—————————————————————————————————————————————————————————————————————————————————————
  141. //    VULongNumber::Clone    -    returns a copy of the VULongNumber object
  142. //—————————————————————————————————————————————————————————————————————————————————————
  143. ScriptValue*
  144. VULongNumber::Clone()
  145. {
  146.     return new VULongNumber( fNumber );
  147. }
  148.  
  149. //—————————————————————————————————————————————————————————————————————————————————————
  150. //                                VUString
  151. //—————————————————————————————————————————————————————————————————————————————————————
  152. //    VUString::VUString    -    constructor.
  153. //—————————————————————————————————————————————————————————————————————————————————————
  154. VUString::VUString( char* pText )
  155. {
  156.     fValueKind = kVUStringKind;
  157.     fText = new char[ strlen( pText ) + 1];     //————    allocate a string buffer
  158.     if( fText )
  159.     {
  160.         strcpy( fText, pText );
  161.     }
  162. }
  163.  
  164. //—————————————————————————————————————————————————————————————————————————————————————
  165. //    VUString::~VUString    -    destructor.
  166. //—————————————————————————————————————————————————————————————————————————————————————
  167. VUString::~VUString()
  168. {
  169.         //————    delete the string buffer
  170.     if( fText )
  171.     {
  172.         delete fText;
  173.     }
  174. }
  175.  
  176. //—————————————————————————————————————————————————————————————————————————————————————
  177. //    VUString::GetText    -    returns a pointer the text.
  178. //—————————————————————————————————————————————————————————————————————————————————————
  179.  char* 
  180.  VUString::GetText()
  181. {
  182.     return fText;
  183. }
  184.  
  185. //—————————————————————————————————————————————————————————————————————————————————————
  186. //    VUString::GetTextSize    -    returns the number of characters in the string
  187. //—————————————————————————————————————————————————————————————————————————————————————
  188. unsigned short
  189. VUString::GetTextSize()
  190. {
  191.     if( fText )
  192.     {
  193.         return strlen( fText );
  194.     }
  195.     else
  196.     {
  197.         return 0;
  198.     }
  199. }
  200.  
  201. //—————————————————————————————————————————————————————————————————————————————————————
  202. //    VUString::Clone    -    returns a copy of the VUString object
  203. //—————————————————————————————————————————————————————————————————————————————————————
  204. ScriptValue*
  205. VUString::Clone()
  206. {
  207.     return new VUString( fText );
  208. }
  209.  
  210. //—————————————————————————————————————————————————————————————————————————————————————
  211. //                                VUList
  212. //—————————————————————————————————————————————————————————————————————————————————————
  213. //    VUList::VUList    -    create an empty VUList.
  214. //—————————————————————————————————————————————————————————————————————————————————————
  215. VUList::VUList()
  216. {
  217.     fValueKind = kVUListKind;
  218.     fListHead = nil;
  219.     fCount = 0;
  220. }
  221.  
  222. //—————————————————————————————————————————————————————————————————————————————————————
  223. //    VUList::~VUList    -    destructor.
  224. //—————————————————————————————————————————————————————————————————————————————————————
  225. VUList::~VUList()
  226. {
  227.     ScriptValue*    tValue = fListHead;
  228.     ScriptValue*    tNextValue;
  229.  
  230.     while( tValue != nil )
  231.     {
  232.         tNextValue = tValue->GetNextValue();
  233.         delete tValue;
  234.         tValue = tNextValue;
  235.     }
  236. }
  237.  
  238. //—————————————————————————————————————————————————————————————————————————————————————
  239. //    VUList::GetCount    -    return the number of elements in the list.
  240. //—————————————————————————————————————————————————————————————————————————————————————
  241. short
  242. VUList::GetCount()
  243. {
  244.     return fCount;
  245. }
  246.  
  247. //—————————————————————————————————————————————————————————————————————————————————————
  248. //    VUList::GetNthItem    -    return the number of elements in the list.
  249. //—————————————————————————————————————————————————————————————————————————————————————
  250. OSErr
  251. VUList::GetNthItem( short pIndex, ScriptValuePtr& pItemPtr, ValueKind& pKind )
  252. {
  253.     ValueKind        tKind;
  254.     
  255.         //————    Get a pointer to the item
  256.     pItemPtr = GetNthItem( pIndex );
  257.     if( pItemPtr == nil )
  258.     {
  259.         pKind = kVUNullKind;
  260.         return errAEWrongParameters; 
  261.     }
  262.  
  263.         //————    Check the item's kind
  264.     tKind = pItemPtr->GetValueKind();
  265.     if( (tKind == pKind) || (pKind == kVUAnyKind) )
  266.     {
  267.         pKind = tKind;
  268.         return noErr; 
  269.     }
  270.     else
  271.     {
  272.         pItemPtr = nil;
  273.         return errAEWrongParameters;
  274.     }
  275. }
  276.  
  277. //—————————————————————————————————————————————————————————————————————————————————————
  278. //    VUList::GetNthItem    -    return the Nth item in the list as a ScriptValue
  279. //—————————————————————————————————————————————————————————————————————————————————————
  280. ScriptValue*
  281. VUList::GetNthItem( short pIndex )
  282. {
  283.     short            tCurIndex;
  284.     ScriptValue*    tValue;
  285.     ScriptValue*    tNextValue;
  286.     
  287.         //————    Make sure we have a list to traverse
  288.     if( fListHead != nil )
  289.     {
  290.             //————    Begin with the first item
  291.         tCurIndex = 1;
  292.         tValue = fListHead;
  293.  
  294.             //————    Traverse the list until the Nth item is found,
  295.             //————    or we reach the end of the list
  296.         while( tCurIndex < pIndex )
  297.         {
  298.                 //————    Get the next item in the list
  299.             tNextValue = tValue->GetNextValue();
  300.             if( tNextValue != nil )
  301.             {
  302.                     //————    We have a next item, increment count and advance pointer
  303.                 tValue = tNextValue;
  304.                 tCurIndex++;
  305.             }
  306.             else
  307.             {
  308.                     //————    We have reached the end of the list,
  309.                     //————    without getting to the Nth item.
  310.                     //————    return nil object pointer
  311.                 return nil;
  312.             }
  313.                 //————    We have reached the indexed item
  314.                 //————    return its pointer
  315.         }
  316.         return tValue;
  317.     }
  318.     else
  319.     {
  320.             //————    This list is empty, return nil object pointer
  321.          return nil;
  322.     }
  323. }
  324.  
  325. //—————————————————————————————————————————————————————————————————————————————————————
  326. //    VUList::PutNthItem    -    insert the ScriptValue in the Nth position of the list,
  327. //—————————————————————————————————————————————————————————————————————————————————————
  328. void
  329. VUList::PutNthItem( ScriptValue* pValue, short pIndex )
  330. {
  331.     short            tCurIndex;
  332.     ScriptValue*    tValue;
  333.     ScriptValue*    tNextValue;
  334.     
  335.         //————    Is this list empty
  336.     if( fListHead == nil )
  337.     {
  338.             //————    The list is empty, append item as kludgy work around
  339.         fListHead = pValue;
  340.         pValue->SetNextValue( nil );
  341.             
  342.             //————    Keep track of how many items in the list
  343.         fCount = 1;
  344.         return;
  345.     }
  346.     else
  347.     {
  348.             //———— The list is not empty, traverse list to pIndex item    
  349.         tValue = fListHead;
  350.         tCurIndex = 1;
  351.         while( tCurIndex < pIndex )
  352.         {
  353.                 //————    Get the next item in the list
  354.             tNextValue = tValue->GetNextValue();
  355.             if( tNextValue != nil )
  356.             {
  357.                     //————    the next item exists, advance pointer and increment tCurIndex
  358.                 tValue = tNextValue;
  359.                 tCurIndex++;
  360.             }
  361.             else
  362.             {
  363.                     //————    the next item does not exist, append item as kludgy work around
  364.                 tValue->SetNextValue( pValue );
  365.                 pValue->SetNextValue( nil );
  366.  
  367.                     //————    Keep track of how many items in the list
  368.                 fCount++;
  369.                 return;
  370.             }
  371.         }
  372.  
  373.             //————    We have found the indexed item, insert pValue at this position
  374.         pValue->SetNextValue( tValue->GetNextValue() );
  375.         tValue->SetNextValue( pValue );
  376.  
  377.             //————    Keep track of how many items in the list
  378.         fCount++;
  379.         return;
  380.     }
  381. }
  382.  
  383. //—————————————————————————————————————————————————————————————————————————————————————
  384. //    VUList::PutNthItem    -    insert the boolean at the Nth position of the list
  385. //—————————————————————————————————————————————————————————————————————————————————————
  386. void
  387. VUList::PutNthItem( Boolean pFlag, short pIndex )
  388. {
  389.     VUBoolean*    tBooleanVal;
  390.     
  391.     tBooleanVal = new VUBoolean( pFlag );
  392.     if( tBooleanVal )
  393.     {
  394.         this->PutNthItem( tBooleanVal, pIndex );
  395.     }
  396. }
  397.  
  398. //—————————————————————————————————————————————————————————————————————————————————————
  399. //    VUList::PutNthItem    -    insert the number at the Nth position of the list
  400. //—————————————————————————————————————————————————————————————————————————————————————
  401. void
  402. VUList::PutNthItem( short pNumber, short pIndex )
  403. {
  404.     VUNumber*    tNumVal;
  405.     
  406.     tNumVal = new VUNumber( pNumber );
  407.     if( tNumVal )
  408.     {
  409.         this->PutNthItem( tNumVal, pIndex );
  410.     }
  411. }
  412.  
  413. //—————————————————————————————————————————————————————————————————————————————————————
  414. //    VUList::PutNthItem    -    insert the long number at the Nth position of the list
  415. //—————————————————————————————————————————————————————————————————————————————————————
  416. void
  417. VUList::PutNthItem( long pNumber, short pIndex )
  418. {
  419.     VULongNumber*    tLongVal;
  420.     
  421.     tLongVal = new VULongNumber( pNumber );
  422.     if( tLongVal )
  423.     {
  424.         this->PutNthItem( tLongVal, pIndex );
  425.     }
  426. }
  427.  
  428. //—————————————————————————————————————————————————————————————————————————————————————
  429. //    VUList::PutNthItem    -    insert the string at the Nth position of the list
  430. //—————————————————————————————————————————————————————————————————————————————————————
  431. void
  432. VUList::PutNthItem( char* pString, short pIndex )
  433. {
  434.     VUString*    tStrVal;
  435.     
  436.     tStrVal = new VUString( pString );
  437.     if( tStrVal )
  438.     {
  439.         this->PutNthItem( tStrVal, pIndex );
  440.     }
  441. }
  442.  
  443. //—————————————————————————————————————————————————————————————————————————————————————
  444. //    VUList::Clone    -    returns a copy of the VUList object
  445. //—————————————————————————————————————————————————————————————————————————————————————
  446. ScriptValue*
  447. VUList::Clone()
  448. {
  449.     VUList*            tList;
  450.     short            i;
  451.     ScriptValue*    tVal;
  452.     ScriptValue*    tVal2;
  453.     
  454.     tList = new VUList();
  455.     if( tList )
  456.     {
  457.         for( i = 1; i <= fCount; i++ )
  458.         {
  459.             tVal = GetNthItem( i );
  460.             tVal2 = tVal->Clone();
  461.             if( tVal2 )
  462.             {
  463.                 tList->PutNthItem( tVal2, i );
  464.             }
  465.         }
  466.     }
  467.     return tList;
  468. }
  469.  
  470.